package com.dswz.controller.pc;

import com.dswz.po.User;
import com.dswz.service.SessionService;
import com.dswz.service.UserService;
import com.dswz.utils.AdminResult;
import com.dswz.utils.EncryptUtil;
import com.dswz.utils.RequestUtils;
import com.dswz.utils.Utils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({ "/pc" })
public class UserPcController {
	private Logger logger = LogManager.getLogger(UserPcController.class);

	@Autowired
	private UserService userService;
	@Autowired
	private SessionService sessionService;

	@RequestMapping({ "/toLogin" })
	@ResponseBody
	public Object toLogin(HttpServletRequest request, HttpServletResponse response, Model model) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			String username = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"USER_SESSION");
			this.logger.info("页面来源:" + request.getHeader("referer") + "客户端:" + request.getHeader("user-agent")
					+ " 客户端ip: " + Utils.getUserRealAddress(request) + " 操作人: " + username);
			this.logger.error("用户未登录!");
			result.setResult(1);
			result.setMessage("用户未登录");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}
		return result;
	}

	@RequestMapping(value = { "/userLogin" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	@ResponseBody
	public Object userLogin(String userMobile, String userPassword, String code, Model model,
			HttpServletRequest request, HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			this.logger.info("userMobile:" + userMobile + "userPassword:" + userPassword + "code:" + code + "页面来源:"
					+ request.getHeader("referer") + "客户端:" + request.getHeader("user-agent") + " 客户端ip: "
					+ Utils.getUserRealAddress(request));
			if (StringUtils.isEmpty(userMobile)) {
				this.logger.error("请输入手机号!");
				result.setMessage("请输入手机号!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(userPassword)) {
				this.logger.error("请输入密码!");
				result.setMessage("请输入密码!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(code)) {
				this.logger.error("请输入验证码!");
				result.setMessage("请输入验证码!");
				result.setResult(3);
				return result;
			}

			String validateCode = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"VALIDATECODE_SESSION");
			if (StringUtils.isEmpty(validateCode)) {
				this.logger.error("请获取验证码!");
				result.setMessage("请获取验证码!");
				result.setResult(3);
				return result;
			}
			if (!validateCode.equalsIgnoreCase(code)) {
				this.logger.error("验证码错误!");
				result.setMessage("验证码错误!");
				result.setResult(3);
				return result;
			}

			User user = this.userService.getUser(userMobile, Byte.valueOf((byte) 0));
			if (user == null) {
				this.logger.error("手机号不正确!");
				result.setMessage("手机号不正确!");
				result.setResult(3);
				return result;
			}
			if (EncryptUtil.encrypt(EncryptUtil.getDefaultKey(), userPassword).equals(user.getUserLoginPassword())) {
				this.sessionService.setAttribute(/* 126 */ RequestUtils.getCSESSIONID(request, response),
						"USER_SESSION", user.getUserMobile());
			} else {
				this.logger.error("密码不正确!");
				result.setMessage("密码不正确!");
				result.setResult(3);
				return result;
			}

			Map<String, Object> map = new HashMap();
			map.put("userMobile", userMobile.substring(0, 3) + "****" + userMobile.substring(7, 11));
			map.put("userId", user.getUserId());
			result.setData(map);
			result.setResult(0);
			result.setMessage("OK");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}
		return result;
	}

	@RequestMapping(value = { "/userRegister" }, method = {
			org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public Object userRegister(String userMobile, String userLoginPassword, String code, String confirmPassword,
			Model model, HttpServletRequest request, HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			this.logger.info("userMobile:" + userMobile + "userLoginPassword:" + userLoginPassword + "code:" + code
					+ "confirmPassword:" + confirmPassword + "页面来源:" + request.getHeader("referer") + "客户端:"
					+ request.getHeader("user-agent") + " 客户端ip: " + Utils.getUserRealAddress(request));
			if (StringUtils.isEmpty(userMobile)) {
				this.logger.error("请输入手机号!");
				result.setMessage("请输入手机号!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(userLoginPassword)) {
				this.logger.error("请输入密码!");
				result.setMessage("请输入密码!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(code)) {
				this.logger.error("请输入手机验证码!");
				result.setMessage("请输入手机验证码!");
				result.setResult(3);
				return result;
			}
			if (this.userService.getUser(userMobile, null) != null) {
				this.logger.error("手机号已存在!");
				result.setMessage("手机号已存在!");
				result.setResult(3);
				return result;
			}
			if (!userLoginPassword.equals(confirmPassword)) {
				this.logger.error("两次密码输入不一致!");
				result.setMessage("两次密码输入不一致!");
				result.setResult(3);
				return result;
			}

			String smsValidateCode = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"SMS_REGISTERVCODE_SESSION");
			if (StringUtils.isEmpty(smsValidateCode)) {
				this.logger.error("请获取手机验证码!");
				result.setMessage("请获取手机验证码!");
				result.setResult(3);
				return result;
			}
			if (!smsValidateCode.equalsIgnoreCase(code)) {
				this.logger.error("手机验证码错误!");
				result.setMessage("手机验证码错误!");
				result.setResult(3);
				return result;
			}
			User user = new User();
			user.setUserId(UUID.randomUUID().toString().replaceAll("-", ""));
			user.setUserMobile(userMobile);
			user.setUserLoginPassword(EncryptUtil.encrypt(EncryptUtil.getDefaultKey(), userLoginPassword));
			this.userService.insertUser(user);

			this.sessionService.setAttribute(RequestUtils.getCSESSIONID(request, response), "USER_SESSION",
					user.getUserMobile());
			Map<String, Object> map = new HashMap();
			map.put("userId", user.getUserId());
			map.put("userMobile",
					user.getUserMobile().substring(0, 3) + "****" + user.getUserMobile().substring(7, 11));
			result.setData(map);
			result.setResult(0);
			result.setMessage("OK");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}

		return result;
	}

	@RequestMapping({ "/getRegisterSmsVCode" })
	@ResponseBody
	public Object getRegisterSmsVCode(String userMobile, HttpServletRequest request, HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			this.logger.info("userMobile:" + userMobile + "页面来源:" + request.getHeader("referer") + "客户端:"
					+ request.getHeader("user-agent") + " 客户端ip: " + Utils.getUserRealAddress(request));
			if (StringUtils.isEmpty(userMobile)) {
				this.logger.error("请输入手机号!");
				result.setMessage("请输入手机号!");
				result.setResult(3);
				return result;
			}
			String code = String.valueOf(Math.round(Math.random() * 1000000.0D));
			this.logger.info("短信验证码:" + code);
			JsonObject object = new JsonObject();
			object.addProperty("code", code);
			this.userService.sendSmsValidateCode(userMobile, object.toString(), "427804");

			this.sessionService.setAttribute2(RequestUtils.getCSESSIONID(request, response),
					"SMS_REGISTERVCODE_SESSION", code);
			result.setResult(0);
			result.setMessage("OK");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}
		return result;
	}

	@RequestMapping({ "/getForgetPwdSmsVCode" })
	@ResponseBody
	public Object getForgetPwdSmsVCode(String userMobile, HttpServletRequest request, HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			this.logger.info("userMobile:" + userMobile + "页面来源:" + request.getHeader("referer") + "客户端:"
					+ request.getHeader("user-agent") + " 客户端ip: " + Utils.getUserRealAddress(request));
			if (StringUtils.isEmpty(userMobile)) {
				this.logger.error("请输入手机号!");
				result.setMessage("请输入手机号!");
				result.setResult(3);
				return result;
			}
			User user = this.userService.getUser(userMobile, Byte.valueOf((byte) 0));
			if (user == null) {
				this.logger.error("手机号不存在!");
				result.setMessage("手机号不存在!");
				result.setResult(3);
				return result;
			}
			String code = String.valueOf(Math.round(Math.random() * 1000000.0D));
			this.logger.info("短信验证码:" + code);
			JsonObject object = new JsonObject();
			object.addProperty("code", code);
			this.userService.sendSmsValidateCode(userMobile, object.toString(), "427805");

			this.sessionService.setAttribute2(RequestUtils.getCSESSIONID(request, response),
					"SMS_FORGETPWDVCODE_SESSION", code);
			result.setResult(0);
			result.setMessage("OK");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}
		return result;
	}

	@RequestMapping(value = { "/saveForgetPassword" }, method = {
			org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public Object saveForgetPassword(String userMobile, String code, String newPassword, String confirmPassword,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			this.logger.info("userMobile:" + userMobile + "code:" + code + "newPassword:" + newPassword
					+ "confirmPassword:" + confirmPassword + "页面来源:" + request.getHeader("referer") + "客户端:"
					+ request.getHeader("user-agent") + " 客户端ip: " + Utils.getUserRealAddress(request));
			if (StringUtils.isEmpty(userMobile)) {
				this.logger.error("请输入手机号!");
				result.setMessage("请输入手机号!");
				result.setResult(3);
				return result;
			}
			User user = this.userService.getUser(userMobile, Byte.valueOf((byte) 0));
			if (user == null) {
				this.logger.error("手机号不存在!");
				result.setMessage("手机号不存在!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(newPassword)) {
				this.logger.error("请输入新密码!");
				result.setMessage("请输入新密码!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(confirmPassword)) {
				this.logger.error("请输入确认密码!");
				result.setMessage("请输入确认密码!");
				result.setResult(3);
				return result;
			}
			if (!newPassword.equals(confirmPassword)) {
				this.logger.error("确认密码与新密码不一致!");
				result.setMessage("确认密码与新密码不一致!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(code)) {
				this.logger.error("请输入手机验证码!");
				result.setMessage("请输入手机验证码!");
				result.setResult(3);
				return result;
			}
			String smsValidateCode = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"SMS_FORGETPWDVCODE_SESSION");
			if (StringUtils.isEmpty(smsValidateCode)) {
				this.logger.error("请获取手机验证码!");
				result.setMessage("请获取手机验证码!");
				result.setResult(3);
				return result;
			}
			if (!smsValidateCode.equalsIgnoreCase(code)) {
				this.logger.error("手机验证码错误!");
				result.setMessage("手机验证码错误!");
				result.setResult(3);
				return result;
			}
			user.setUserLoginPassword(EncryptUtil.encrypt(EncryptUtil.getDefaultKey(), confirmPassword));
			this.userService.updateUser(user);
			result.setResult(0);
			result.setMessage("OK");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}
		return result;
	}
}
